package com.pro.springbootshop.controller;

import com.pro.springbootshop.entity.Buyer;
import com.pro.springbootshop.entityExtend.BuyerReturn;
import com.pro.springbootshop.service.BuyerService;
import com.pro.springbootshop.util.TokenUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class BuyerController {
    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    BuyerService buyerService;
    @ApiOperation("买家注册")
    @PostMapping("/api/buyer/register")
    public String saveBuyer(@RequestBody Buyer buyer){
        return buyerService.saveBuyer(buyer);
    }
    @ApiOperation("买家登录")
    @PostMapping("/api/buyer/login")
    public String loginBuyer(@RequestBody Buyer buyer){
        return buyerService.loginBuyer(buyer);
    }
    @ApiOperation("获取买家信息")
    @GetMapping("/api/buyer")
    public Map<String,String> getBuyer(HttpServletRequest request){
        String token = request.getHeader("token");
        return buyerService.getBuyer(token);
    }
    @ApiOperation("修改买家信息")
    @PostMapping("/api/buyer/detail")
    public String updateBuyer(@RequestBody Buyer buyer){
        return buyerService.updateBuyer(buyer);
    }
    @ApiOperation("修改买家密码")
    @PostMapping("/api/buyer/detail/{oldBuyerPw}/{buyerPw}")
    public String updateBuyerPw(@PathVariable(name = "oldBuyerPw") String oldBuyerPw,@PathVariable(name = "buyerPw") String buyerPw, HttpServletRequest request){
        String token = request.getHeader("token");
        return buyerService.updateBuyerPw(oldBuyerPw,buyerPw,token);
    }
    @ApiOperation("商家分页获取所有买家")
    @GetMapping("/api/buyer/{page}/{limit}")
    public BuyerReturn sellerGetBuyerIPage(@PathVariable(name = "page") Integer page, @PathVariable(name = "limit") Integer limit){
        return buyerService.sellerGetBuyerIPage(page,limit);
    }
    @ApiOperation("商家获取所有买家")
    @GetMapping("/api/seller/buyer")
    public List<Buyer> sellerGetBuyerList(){
        return buyerService.sellerGetBuyerList();
    }
}
